﻿function IsLeapYear(year) {
    if (year % 4 != 0) {
        return false;
    }
    else {
        if (year % 400 == 0) {
            return true;
        }
        else {
            if (year % 100 == 0) {
                return false;
            }
            else {
                return true;
            }
        }
    }
}

var ProcessDate = {
    MonthYearChange: function () {
        $('select[name="Year"], select[name="Month"]').change(function () {
            var year = $('select[name="Year"]').val();
            var month = $('select[name="Month"]').val();
            var day = 0;
            var str = '<option value="0">Ngày</option>';
            switch (month) {
                case "1":
                    day = 31;
                    break;
                case "2":
                    if (IsLeapYear(year)) {
                        day = 29;
                    }
                    else {
                        day = 28;
                    }

                    break;
                case "3":
                    day = 31;
                    break;
                case "4":
                    day = 30;
                    break;
                case "5":
                    day = 31;
                    break;
                case "6":
                    day = 30;
                    break;
                case "7":
                    day = 31;
                    break;
                case "8":
                    day = 31;
                    break;
                case "9":
                    day = 30;
                    break;
                case "10":
                    day = 31;
                    break;
                case "11":
                    day = 30;
                    break;
                case "12":
                    day = 31;
                    break;
            }

            for (var i = 1; i <= day; i++) {
                str += "<option value=" + i + ">" + i + "</option>";

            }

            var selectedDay = $('select[name="Day"]').val();
            $('select[name="Day"]').html(str);
            if (selectedDay <= day) {
                $('select[name="Day"]').val(selectedDay);
            }
        });
    },

    init: function () {
        ProcessDate.MonthYearChange();
    }
}